package com.mrdeer.merchant.controller;

import com.mrdeer.merchant.common.util.SecurityUtil;
import com.mrdeer.merchant.convert.MerchantDetailConvert;
import com.mrdeer.merchant.convert.MerchantRegisterConvert;
import com.mrdeer.merchant.service.FileService;
import com.mrdeer.merchant.vo.MerchantDetailVO;
import com.mrdeer.merchant.vo.MerchantRegisterVO;
import com.mrdeer.merchant.api.MerchantService;
import com.mrdeer.merchant.api.dto.MerchantDTO;
import com.mrdeer.user.api.TenantService;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author ：xie jianchu
 * @date ：Created in 2020/9/14 0014 9:24
 */
@Api(tags = "商户接口-商户管理接口")
@RestController
public class MerchantController {
    /**
     * Dubbo生成代理对象，通过代理对象进行远程调用
     */
    @Reference
    private MerchantService merchantService;

    @Autowired
    private FileService fileService;

    @Reference
    private TenantService tenantService;

    @GetMapping("merchants/{id}")
    @ApiOperation(value = "根据ID获取商户信息")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long id) {
        return merchantService.queryMerchantById(id);
    }

    @ApiOperation(value = "获取当前登录用户的商户信息")
    @GetMapping(value = "/my/merchants")
    public MerchantDTO getMyMerchantInfo() {
        //从Token中获取商户的ID
        Long merchantId = SecurityUtil.getMerchantId();
        return merchantService.queryMerchantById(merchantId);
    }

    @ApiOperation(value = "商户注册")
    @PostMapping("/merchants/register")
    @ApiImplicitParam(value = "商户注册信息", name = "merchantRegisterVO", required = true, paramType = "")
    public MerchantRegisterVO registerMerchant(@RequestBody MerchantRegisterVO merchantRegisterVO) {
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2Dto(merchantRegisterVO);
        merchantService.createMerchant(merchantDTO);
        return MerchantRegisterConvert.INSTANCE.dto2Vo(merchantDTO);
    }

    @ApiOperation("上传证件照")
    @PostMapping("/upload")
    public String update(@ApiParam(value = "上传的文件", required = true) @RequestParam("file") MultipartFile multipartFile) throws IOException {
        String originalFileName = multipartFile.getOriginalFilename();
        String suffix = originalFileName.substring(originalFileName.lastIndexOf(".") - 1);
        //构建生成一个文件名称，需要保证这个文件名称唯一
        String fileName = UUID.randomUUID() + "-" + System.currentTimeMillis() + suffix;
        //调用fileService
        return fileService.upload(multipartFile.getBytes(), fileName);
    }

    @ApiOperation("商户资质申请")
    @PostMapping("/my/merchants/save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantInfo", value = "商户认证材料", required = true, dataType = "MerchantDetailVO", paramType = "body")
    })
    public void saveMerchant(@RequestBody MerchantDetailVO merchantInfo) {
        //TODO:解析Token，取出Token中携带过来的商户ID
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchantDTO = MerchantDetailConvert.INSTANCE.vo2Dto(merchantInfo);
        merchantService.applyMerchant(merchantId, merchantDTO);
    }
}
